using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TMPro;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class DialogueSystemManager : SingletonMono<DialogueSystemManager>
{
    [Header("UI组件")]
    [SerializeField] private TMP_Text dialogueUIText;
    [SerializeField] private Image dialogueUIImage;


    //  字典，保存对话内容，多次执行同一对话，减少加载次数
    private Dictionary<string, string[]> dialogueTextListDic = new Dictionary<string, string[]>();

    //
    private bool isDialogue = true;            // 是否可以进行继续对话
    private int dialogueTextLength;            // 对话文件长度
    private int index;                         // 对话索引


    /// <summary>
    /// 设置对话组件
    /// 场景加载后，由UI系统调用
    /// </summary>
    /// <param name="tMP_Text">UI文本组件</param>
    /// <param name="image">UI任务头像</param>
    public void SetDialogue(TMP_Text tMP_Text, Image image)
    {
        dialogueUIText = tMP_Text;
        dialogueUIImage = image;
    }

    /********************************/


    /// <summary>
    /// 在Update函数中调用，持续检测
    /// </summary>
    /// <param name="isCanDialogue">是否可以对话，进入碰撞体后</param>
    /// <param name="dialogueFilePath"></param>
    public void StartDialogue(string dialogueFilePath, bool isCanDialogue)
    {
        if(isCanDialogue)
        {
            if(GlobalUIInput.Instance.isDialogue && isDialogue)
            {
                ShowDialogue(dialogueFilePath, index);
                index++;
            }
            if(index > dialogueTextLength-1)
            {
                index = 0;
            }
        }
    }

    /// <summary>
    /// 刚进入碰撞体时调用
    /// </summary>
    /// <param name="dialogueFilePath">对话文件路径</param>
    public void StartDialogue(string dialogueFilePath, Sprite sprite)
    {
        dialogueUIText.gameObject.SetActive(true);     // 打开对话框
        dialogueUIImage.gameObject.SetActive(true);    // 关闭对话头像

        if(sprite != null)
            dialogueUIImage.sprite = sprite;           // 设置对话头像
        
        ShowDialogue(dialogueFilePath, index);         // 开始对话
        index++;                                       // 索引自增
    }

    /// <summary>
    /// 离开碰撞体结束时调用
    /// </summary>
    public void StartDialogue()
    {
        index = 0;                                     // 对话索引初始化
        dialogueUIImage.sprite = null;                 // 头像设置为空
        dialogueUIText.gameObject.SetActive(false);    // 关闭对话框
        dialogueUIImage.gameObject.SetActive(false);   // 关闭对话头像
    }

    /// <summary>
    /// 将数据传递给UI面板
    /// </summary>
    /// <param name="dialogueFilePath">文件路径</param>
    /// <param name="index">读取文件第几行</param>
    private void ShowDialogue(string dialogueFilePath, int index)
    {   
        if(isDialogue)
            StartCoroutine(ShowText(dialogueFilePath, index));
    }

    /// <summary>
    /// 协程
    /// 将文字一个一个打出
    /// </summary>
    /// <param name="dialogueFilePath">文件路径</param>
    /// <param name="index">索引，输出第几行</param>
    /// <returns></returns>
    IEnumerator ShowText(string dialogueFilePath, int index)
    {
        isDialogue = false;
        dialogueUIText.text = "";
        for(int i = 0 ; i < ReadTextFile(dialogueFilePath)[index].Length ; i++)
        {
            dialogueUIText.text += ReadTextFile(dialogueFilePath)[index][i];
            yield return new WaitForSecondsRealtime(0.1f);
        }
        isDialogue = true;
    }

    /// <summary>
    /// 读取文件
    /// 在字典内查找，有直接返回
    /// 没有，加载文件再返回
    /// </summary>
    /// <param name="dialogueFilePath"></param>
    /// <returns></returns>
    private string[] ReadTextFile(string dialogueFilePath)
    {
        if(!dialogueTextListDic.ContainsKey(dialogueFilePath))
            dialogueTextListDic.Add(dialogueFilePath, LoadDialogueText(dialogueFilePath));

        return dialogueTextListDic[dialogueFilePath];
    }

    /// <summary>
    /// 加载文本文件
    /// 返回string数组
    /// 计算文本有多少行
    /// </summary>
    /// <param name="dialogueFilePath">对话文件路径</param>
    /// <returns></returns>
    private string[] LoadDialogueText(string dialogueFilePath)
    {
        string[] textString = File.ReadAllLines(Application.dataPath + dialogueFilePath + ".txt");

        // 如何读取文件为空，设置为默认对话文本
        if(textString == null)
            textString = File.ReadAllLines(Application.dataPath + "/Dialogue/Text/Acquiesce/Acquiesce.txt");
        
        dialogueTextLength = textString.Length;
        return textString;
    }
}
